create or replace function updateDistributor(
	out xml, inout xml
	)
as
$$
	declare
		-- alias is name for stored procedure parameters
		executeStatusData 	alias for $1;
		distributorData		alias for $2;
		
		-- variable for node data
		vIdDistributor 		bigint := null; -- distributor id in database
		vNameDistributor				text := null; 	-- distributor name
		
		-- node path
		vIdNode						varchar(100) := '/distributor/@id';
		vNameNode					varchar(100) := '/distributor/distributorname';
		
		
	begin
		-- get data from xml
		vIdDistributor := cast(extract_value(vIdNode, DistributorData) as bigint);
		vNameDistributor := extract_value(vNameNode, DistributorData);

		-- first insert into trace table previous data
		insert into DistributorTrace(idDistributor, nameDistributor)
			select idDistributor, nameDistributor from Distributor dior
				where
					dior.idDistributor = vIdDistributor
		;
		
		-- update data in table
		update Distributor dior
			set
				nameDistributor = vNameDistributor
			where
				dior.idDistributor = vIdDistributor
		;
			
		executeStatusData := statusCall(successcode(), updateText()); -- status data
		return;

	end;
$$ language plpgsql VOLATILE;